import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { resolve } from 'path'
import vueSetupExtend from 'vite-plugin-vue-setup-extend' // 设置neme属性
import AutoImport from 'unplugin-auto-import/vite' // 自动导入
import viteCompression from 'vite-plugin-compression' // 静态资源压缩
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import prismjs from 'vite-plugin-prismjs';

import Unocss from 'unocss/vite'
import {
  presetAttributify,
  presetIcons,
  presetUno,
  transformerDirectives,
  transformerVariantGroup,
} from 'unocss'
export default defineConfig({
  // 配置NG——这个是与后台、运维约定好的，做NG转发；即主应用地址+子应用base就会直接NG转发
  base: '/',
  define: {
    'process.env': {
      "VUE_IMG_BASE_URL":"http://www.lihynotes.cn:81/",
      "VITE_APP_BASE_API":"http://111.67.202.251:3002"
    }
  },
  resolve: {
    // 配置别名
    alias: {
      '@': resolve(__dirname, 'src'),
      // components: resolve(__dirname, 'src/components')
    },
    // 类型： string[] 导入时想要省略的扩展名列表。
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.mjs','.d.ts']
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    },
  },
  plugins: [
    vue(),
    vueJsx(),
    vueSetupExtend(),
    AutoImport({
      imports: ['vue', 'vue-router','pinia'], // 自动导入vue、vue-router、pinia相关API
      dts: 'src/auto-import.d.ts' // 生成 `auto-import.d.ts` 全局声明
    }),
    viteCompression({
      verbose: true,
      disable: false, // 不禁用压缩
      deleteOriginFile: false, // 压缩后是否删除原文件
      threshold: 10240, // 压缩前最小文件大小
      algorithm: 'gzip', // 压缩算法
      ext: '.gz' // 文件类型
    }),
    Components({
      // allow auto load markdown components under `./src/components/`
      extensions: ['vue', 'md'],
      // allow auto import and register components used in markdown
      include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
      dts: 'src/components.d.ts',
    }),

    // https://github.com/antfu/unocss
    // see unocss.config.ts for config
    Unocss({
      presets: [
        presetUno(),
        presetAttributify(),
        presetIcons({
          scale: 1.2,
          warn: true,
        }),
      ],
      transformers: [
        transformerDirectives(),
        transformerVariantGroup(),
      ]
    }),
  ],
  server: {
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    host: '0.0.0.0',
    port: 8080,
    open: true,
    https: false,
    proxy: {
      '^/api': {
        target: 'http://111.67.202.251:3002',
        changeOrigin: true,
        rewrite: (p) => p.replace(/^\/api/, '')
      },
      // '^/portal-user': {
      //   target: `http://10.0.10.240:8172`,
      //   changeOrigin: true,
      //   rewrite: (p) => p.replace(/^\/portal-user/, '/portal-user')
      // },
      // '^/portal-sso': {
      //   target: `http://10.0.10.240:8171`,
      //   changeOrigin: true,
      //   rewrite: (p) => p.replace(/^\/portal-sso/, '/portal-sso')
      // }
    }
  }
})
